/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.j002.requirementengineeringmuni.impl;

import cz.muni.fi.j002.requirementengineeringmuni.Bank;
import cz.muni.fi.j002.requirementengineeringmuni.exceptions.BankException;
import java.math.BigDecimal;

/**
 *
 * @author stefan
 */
public class BankImpl implements Bank {
    
    private BigDecimal accountState = BigDecimal.ZERO;
    
    /**
     * Creates a new BankImpl with a provided initial amount (which must be a positive value)
     * @param initialState 
     */
    public BankImpl(BigDecimal initialState) {
        try {
            this.increaseAccountState(initialState);
        } catch(BankException ex) {
            throw new IllegalArgumentException(ex);
        }
    }

    @Override
    public BigDecimal getAccountState() {
        return accountState;
    }

    @Override
    public void increaseAccountState(BigDecimal amount) throws BankException {
        if(amount.signum() == -1) {
            throw new BankException("amount less than zero (" + amount + ")");
        }
        
        accountState = accountState.add(amount);
    }

    @Override
    public void decreaseAccoungState(BigDecimal amount) throws BankException {
        if(amount.signum() == -1) {
            throw new BankException("amount less than zero (" + amount + ")");
        }
        if(accountState.compareTo(amount) == -1) {
            throw new BankException("account state (" + accountState + ") cannot be decreased for the provided amount (" + amount + ")");
        }
        
        accountState = accountState.subtract(amount);
    }
    
}
